home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 9 / Engine / AnimatedObject.cpp next >
Encoding:
C/C++ Source or Header  |  2004-10-21  |  5.0 KB  |  132 lines

  1. //-----------------------------------------------------------------------------
  2. // AnimatedObject.h implementation.
  3. // Refer to the AnimatedObject.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The animated object class constructor.
  12. //-----------------------------------------------------------------------------
  13. AnimatedObject::AnimatedObject( char *meshName, char *meshPath, unsigned long type ) : SceneObject( type, meshName, meshPath, false )
  14. {
  15.     // Create a clone of the mesh's animation controller.
  16.     if( GetMesh() != NULL )
  17.         GetMesh()->CloneAnimationController( &m_animationController );
  18.     else
  19.         m_animationController = NULL;
  20.  
  21.     // Set the track speed on both tracks to full speed.
  22.     if( m_animationController != NULL )
  23.     {
  24.         m_animationController->SetTrackSpeed( 0, 1.0f );
  25.         m_animationController->SetTrackSpeed( 1, 1.0f );
  26.     }
  27.  
  28.     // Clear the variables used for animation.
  29.     m_currentTrack = 0;
  30.     m_currentTime = 0.0f;
  31. }
  32.  
  33. //-----------------------------------------------------------------------------
  34. // The animated object class destructor.
  35. //-----------------------------------------------------------------------------
  36. AnimatedObject::~AnimatedObject()
  37. {
  38.     SAFE_RELEASE( m_animationController );
  39. }
  40.  
  41. //-----------------------------------------------------------------------------
  42. // Updates the object.
  43. //-----------------------------------------------------------------------------
  44. void AnimatedObject::Update( float elapsed, bool addVelocity )
  45. {
  46.     // Allow the base scene object to update.
  47.     SceneObject::Update( elapsed, addVelocity );
  48.  
  49.     // Check if the object has an animation controller.
  50.     if( m_animationController )
  51.     {
  52.         // Advance the object's animation controller.
  53.         m_animationController->AdvanceTime( elapsed, this );
  54.  
  55.         // Keep track of the current time for animation purposes.
  56.         m_currentTime += elapsed;
  57.     }
  58.  
  59.     // Update the mesh.
  60.     if( GetMesh() != NULL )
  61.         GetMesh()->Update();
  62. }
  63.  
  64. //-----------------------------------------------------------------------------
  65. // Plays the given animation with the given transition time.
  66. //-----------------------------------------------------------------------------
  67. void AnimatedObject::PlayAnimation( unsigned int animation, float transitionTime, bool loop )
  68. {
  69.     // Ensure object has a valid animation controller.
  70.     if( m_animationController == NULL )
  71.         return;
  72.  
  73.     // Ensure the transition time is always greater than zero.
  74.     if( transitionTime <= 0.0f )
  75.         transitionTime = 0.000001f;
  76.  
  77.     // Find which track to play the new animation on.
  78.     unsigned int newTrack = ( m_currentTrack == 0 ? 1 : 0 );
  79.  
  80.     // Get a pointer to the animation set to play.
  81.     ID3DXAnimationSet *as;
  82.     m_animationController->GetAnimationSet( animation, &as );
  83.  
  84.     // Set the animation set to the new track.
  85.     m_animationController->SetTrackAnimationSet( newTrack, as );
  86.  
  87.     // Clear all the events that are currently set on the tracks.
  88.     m_animationController->UnkeyAllTrackEvents( m_currentTrack );
  89.     m_animationController->UnkeyAllTrackEvents( newTrack );
  90.  
  91.     // Check if this animation should be looped or only played once.
  92.     if( loop == true )
  93.     {
  94.         // Transition the new track in over the specified transition time period.
  95.         m_animationController->KeyTrackEnable( m_currentTrack, false, m_currentTime + transitionTime );
  96.         m_animationController->KeyTrackWeight( m_currentTrack, 0.0f, m_currentTime, transitionTime, D3DXTRANSITION_LINEAR );
  97.         m_animationController->SetTrackEnable( newTrack, true );
  98.         m_animationController->KeyTrackWeight( newTrack, 1.0f, m_currentTime, transitionTime, D3DXTRANSITION_LINEAR );
  99.     }
  100.     else
  101.     {
  102.         // Stop the current track, and start the new track without transitioning.
  103.         m_animationController->SetTrackEnable( m_currentTrack, false );
  104.         m_animationController->SetTrackWeight( m_currentTrack, 0.0f );
  105.         m_animationController->SetTrackEnable( newTrack, true );
  106.         m_animationController->SetTrackWeight( newTrack, 1.0f );
  107.         m_animationController->SetTrackPosition( newTrack, 0.0f );
  108.         m_animationController->KeyTrackEnable( newTrack, false, m_currentTime + as->GetPeriod() );
  109.     }
  110.  
  111.     // Release the pointer to the animation set.
  112.     as->Release();
  113.  
  114.     // The new track is now the current track.
  115.     m_currentTrack = newTrack;
  116. }
  117.  
  118. //-----------------------------------------------------------------------------
  119. // Returns a pointer to the object's animation controller.
  120. //-----------------------------------------------------------------------------
  121. ID3DXAnimationController *AnimatedObject::GetAnimationController()
  122. {
  123.     return m_animationController;
  124. }
  125.  
  126. //-----------------------------------------------------------------------------
  127. // Animation callback handler.
  128. //-----------------------------------------------------------------------------
  129. HRESULT CALLBACK AnimatedObject::HandleCallback( THIS_ UINT Track, LPVOID pCallbackData )
  130. {
  131.     return S_OK;
  132. }